package com.yunxi.service.tms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.tms.SFTmsService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.parts.pojo.PrWaybillTransportCreateReqVO;
import com.yunxi.parts.service.EaiWaybillDubboService;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2019/11/28.
 */
@Service
@Qualifier("sftmsServiceImpl")
public class SFTmsServiceImpl implements SFTmsService {

    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    protected static final Logger logger = LoggerFactory.getLogger(SFTmsServiceImpl.class);
    @Autowired
    EaiWaybillDubboService eaiWaybillDubboServiceImpl;

    @Override
    public ApiResult distributeSFTms(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.TMS_PMS_LOGISTICS_INFORMATION_ROAD)) {
            apiResult = saveLogisticsInformation(esbVO);
        } else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult saveLogisticsInformation(EsbVO esbVO) {
        ApiResult apiResult = null;
        Map resultMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
        resultMap.put("zdate",sdf.format(new Date()));
        resultMap.put("ztime",sdf1.format(new Date()));
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("TMS推送配件订单物流信息跟踪给UPS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //内部业务
            JSONObject jsonObject = JSON.parseObject(reqBody);
            List<PrWaybillTransportCreateReqVO> var1= JSON.parseObject(String.valueOf( jsonObject.get("jsonParame")), new TypeReference<ArrayList<PrWaybillTransportCreateReqVO>>() {});
            apiResult=eaiWaybillDubboServiceImpl.transport(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode()==200){
                resultMap.put("zstat","S");
                resultMap.put("zmess","处理成功!");
            }else {
                resultMap.put("zstat","E");
                resultMap.put("zmess","处理失败!");
            }
            apiResult.setData(resultMap);
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(), e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return apiResult;
    }

}
